﻿@model IEnumerable<MVC_No08.Models.Defect>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("添加", "Create")
</p>
@using (Html.BeginForm("Index", "MyHome", FormMethod.Get))
{
    <div>
        <span>所属设备:</span>
        @Html.DropDownList("Name", ViewBag.DropDownListName as List<SelectListItem>);
        <span>缺陷内容:</span>
        @Html.TextBox("Context")
        <span>缺陷状态:</span>
        @Html.RadioButton("Type", "1")<span>未解决</span>
        @Html.RadioButton("Type", "2")<span>已解决</span>
        @Html.RadioButton("Type", "0", true)<span>全部</span>
        <input type="submit" value="查询" />
    </div>
}
<table class="table">
    <tr>
        <th>
            所属设备
        </th>
        <th>
            类型缺陷
        </th>
        <th>
            缺陷内容
        </th>
        <th>
            添加时间
        </th>
        <th>
            缺陷状态
        </th>
        <th>
            处理时间
        </th>
        <th>
            操作
        </th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @{
                    var deviceList = ViewBag.deviceList as Dictionary<int, string>;
                    var name = deviceList.FirstOrDefault(u => u.Key == item.DeviceID).Value;
                }
                @name
            </td>
            <td>
                @{
                    string Type = null;
                    switch (item.DefectType.ToString())
                    {
                        case "1":
                            Type = "紧急";
                            break;
                        case "2":
                            Type = "重要";
                            break;
                        case "3":
                            Type = "一般";
                            break;
                    }
                }
                @Type
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Content)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.AddTime)
            </td>
            <td>
                @{
                    string State = null;
                    switch (item.DefectState.ToString())
                    {
                        case "1":
                            State = "未完成";
                            break;
                        case "2":
                            State = "已完成";
                            break;
                    }
                }
                @State
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.DealTime)
            </td>
            <td>
                <button onclick="Delete(@item.DefectID)">删除</button>
                @if (item.DefectState == 1)
                {
                    <button onclick="Update(@item.DefectID)">处理缺陷</button>
                }
            </td>
        </tr>
    }

</table>

@section scripts{
    <script>
        function Update(DeviceID) {
            $.post("/MyHome/Update", { ID: DeviceID }, function (data) {
                switch (data) {
                    case 0:
                        alert("处理失败");
                        break;
                    case 1:
                        alert("处理成功");
                        location = "/MyHome/Index";
                        break;
                    case 3:
                        alert("未知错误");
                        break;
                }
            })
        }

        function Delete(DeviceID) {
            if (confirm("确定删除吗?")) {
                $.post("/MyHome/Delect", { ID: DeviceID }, function (data) {
                    switch (data) {
                        case 0:
                            alert("删除失败");
                            break;
                        case 1:
                            alert("删除成功");
                            location = "/MyHome/Index";
                            break;
                        case 3:
                            alert("未知错误");
                            break;
                    }
                })
            }
        }
    </script>
}
